package cae.appBar.buttons
{
	import mx.controls.Button;
	import flash.events.MouseEvent;
	import mx.containers.Canvas;
	import mx.containers.VBox;
	import mx.containers.HBox;
	import mx.controls.Label;
	import mx.managers.PopUpManager;
	import flash.geom.Point;
	import mx.core.Application;
	import flash.ui.Mouse;
	import flash.display.DisplayObject;
	import mx.controls.Alert;
	import mx.controls.LinkBar;
	import mx.collections.ArrayCollection;
	import mx.events.ListEvent;
	import mx.events.ItemClickEvent;
	import mx.effects.WipeDown;
	import caurina.transitions.Equations;
	import mx.effects.Move;
	import mx.effects.Resize;
	import mx.controls.MenuBar;
	import flash.events.Event;
	import cae.skins.CustomContainerBorderSkinB;
	import cae.bigmenu.CaeMenuBig;
	import mx.containers.Box;
	import mx.skins.halo.LinkSeparator;
	import cae.windows.CaeWindowsManager;
	import cae.appBar.CaeISelectableButton;
	import cae.windows.CaeWindowEvent;

	/*[Event(name="OPEN_IT",type="cae.appBar.CaeOptionEvent")]*/

	public class CaePopUpMenuButton extends HBox implements CaeISelectableButton
	{
		public var _dataProvider:XML;
		public var menuPopup:HBox;
		private var l:Label;
		
		public var _selected:Boolean = false;
		private var popUpOpen:Boolean = false;
		
		public function CaePopUpMenuButton()
		{
			addEventListener(MouseEvent.MOUSE_OVER, showMenu);
			addEventListener(MouseEvent.MOUSE_OUT, hideMenuIfNoOverPopUp);
			addEventListener(MouseEvent.CLICK, openMenuSection);

			useHandCursor = true;
			buttonMode = true;
			mouseChildren = false;
			
			
			setSkin();
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			if (!l)
			{
				l = new Label();
				l.text = label;
				/*l.useHandCursor = true;
				l.buttonMode = true;
				l.mouseChildren = false;*/
				addChild(l);
			}
			
			
		}
		
		public function set dataProvider(dp:XML):void
		{
			_dataProvider = dp;
		}
		
		public function get dataProvider():XML
		{
			return _dataProvider;
		}
		
		public function set selected(s:Boolean):void
		{
			_selected = s;
			setSkin();
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function setSkin():void
		{
			if(!popUpOpen)
			{
				if(selected)
				{
					alpha = 0;
					
					styleName = "menuTabSelected";
				}
				else
				{
					alpha = 0;
					styleName = "menuTabNotOpen";
				}
			}
			else
			{
				alpha = 1;
				styleName = "menuTabOpen";
			}
		}
		
		private function createPopUpMenu():void
		{
			if(menuPopup)
			{
				menuPopup.removeEventListener(MouseEvent.MOUSE_OUT, hideMenuOnOut);
			}
			
			menuPopup = new HBox();
			menuPopup.styleName = "menuPopup";

			menuPopup.addEventListener(MouseEvent.MOUSE_OUT, hideMenuOnOut);
			 
			
			for each(var o:XML in dataProvider.menuitem)
			{
				var sub:VBox = new VBox();
				
				var b:Label = new Label();
				b.styleName = "subsectionTitle";
				b.text = String(o.@label).toUpperCase();
				
				sub.addChild(b);
				
				var linkBar:LinkBar = new LinkBar();
				linkBar.labelField = "label";
				var dataLinks:ArrayCollection = new ArrayCollection();
				
				for each(var o2:XML in o.menuitem)
				{
					var link:Object = {label:String(o2.@label), className:String(o2.@ventana), dataId:o2.@dataId}
					dataLinks.addItem( link );
				}
				
				linkBar.dataProvider = dataLinks;
				linkBar.direction = "vertical";
				linkBar.addEventListener(ItemClickEvent.ITEM_CLICK, opcionSeleccionada);
				linkBar.setStyle("separatorColor", 0xc2e6f4);
				sub.addChild(linkBar);

				menuPopup.addChild(sub);
			}
		}

		private function showMenu(e:MouseEvent):void
		{
			if(!popUpOpen)
			{
				createPopUpMenu();

				PopUpManager.addPopUp(menuPopup, this);

				var p:Point = this.localToGlobal(new Point(0, this.height));
				
				menuPopup.x = p.x;
				menuPopup.y = p.y;
				
				popUpOpen = true;
				setSkin();
			}
		}
		
		private function closePopUp():void
		{
			PopUpManager.removePopUp(menuPopup);
			popUpOpen = false;
			setSkin();
		}
		
		private function hideMenuIfNoOverPopUp(e:MouseEvent = null):void
		{
			if( this.contentMouseX < 1 || this.contentMouseX > this.width || this.contentMouseY < 0)
				closePopUp()
		}
			
		private function hideMenuOnOut(e:MouseEvent = null):void
		{
			if(menuPopup)
			{
				if( menuPopup.contentMouseX < 5 ||
					menuPopup.contentMouseX > menuPopup.width - 5 ||
					menuPopup.contentMouseY > menuPopup.height - 5 )
					closePopUp();
				else if( menuPopup.contentMouseY < 5 && (this.contentMouseX < 5 || this.contentMouseX > this.width - 5) )
					closePopUp();
			}
		}
			
		private function opcionSeleccionada(e:ItemClickEvent):void
		{
			closePopUp();
			
			var className:String = ArrayCollection(LinkBar(e.target).dataProvider).getItemAt(e.index).className;
			var displayName:String = ArrayCollection(LinkBar(e.target).dataProvider).getItemAt(e.index).label;
			var dataId:Object = ArrayCollection(LinkBar(e.target).dataProvider).getItemAt(e.index).dataId;
			
			dispatchEvent(new CaeWindowEvent(CaeWindowEvent.OPEN, className, displayName, dataId, true));
		}
		
		private function openMenuSection(e:MouseEvent):void
		{
			dispatchEvent(new CaeWindowEvent(CaeWindowEvent.OPEN, "cae.bigmenu.CaeMenuBig", null, dataProvider, true));
		}
		
		
	}
}